﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;

namespace WuxiWTO.Web
{
    public partial class commonNews : System.Web.UI.Page
    {
        public string article_title = "";
        public string author = "";
        public string source = "";
        public string writer = "";
        public string created_date = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            GetArticle();
            if (!IsPostBack)
            {
            }
        }
        WTODataContext ctx = new WTODataContext();
        private void GetArticle()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int aid = int.Parse(Request.QueryString["id"]);
                image_new art = ctx.image_news.SingleOrDefault(p => p.ID == aid);
                if (art != null)
                {
                    article_title = art.news_title;
                   
                    created_date = art.created_date.Value.ToShortDateString();
                    litContent.Text = art.news_content;
                    if (!string.IsNullOrEmpty(art.image_path))
                    {
                        ViewState["file_path"] = art.image_path;
                        btnDownloadFile.Visible = true;
                        this.btnDownloadFile.Text = art.image_path.Substring(art.image_path.LastIndexOf("/") + 1, art.image_path.Length - art.image_path.LastIndexOf("/") - 1);
                    }
                    else
                    {
                        btnDownloadFile.Visible = false;
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "paraerr", "alert('参数传递错误');location='default.aspx';", true);
                return;
            }
        }

        private void GetPrevAndNextArticle(int currentID)
        {
            usp_get_prev_articleResult prev = CategoryInfo.GetPrevArticle(currentID);
            usp_get_next_articleResult next = CategoryInfo.GetNextArticle(currentID);
            if (prev != null)
            {
                this.hyPrev.Text = string.Format("上一篇：<a href='articleDetails.aspx?aid={0}'>{1}</a>", prev.article_id, prev.article_title);
            }
            else
            {
                this.hyPrev.Text = "上一篇：已经没有了";
            }

            if (next != null)
            {
                this.hyNext.Text = string.Format("下一篇：<a href='articleDetails.aspx?aid={0}'>{1}</a>", next.article_id, next.article_title);
            }
            else
            {
                this.hyNext.Text = "下一篇：已经没有了";
            }
        }

        protected void btnDownloadFile_Click(object sender, EventArgs e)
        {
            if (ViewState["file_path"] != null)
            {
                string fileName = ViewState["file_path"].ToString();//客户端保存的文件名
                fileName = fileName.Substring(fileName.LastIndexOf("/") + 1, fileName.Length - fileName.LastIndexOf("/") - 1);
                string filePath = Server.MapPath("~/") + ViewState["file_path"].ToString();//路径

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentType = "application/octet-stream";
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    Response.WriteFile(fileInfo.FullName);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('该文件不存在');", true);
                    return;
                }
            }

        }
    }
}